package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hhit.collaborative_learning.dao.TeamUserDao;
import com.hhit.collaborative_learning.dao.impl.TeamUserDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetTeamActorServlet
 */
@WebServlet("/communication/GetTeamActorServlet")
public class GetTeamActorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetTeamActorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tid=request.getParameter("teamID");
		TeamUserDao tud=new TeamUserDaoImpl();
		JSONObject jobj=new JSONObject();
		Connection con=DBAccess.getInstance().getConn();
		if(!StringUtils.isNullOrEmpty(tid)){
			tid=" teamId ="+tid;
		}
		ResultSet rs=tud.query_user(con, 0, 0, tid, "desc", "CreatedOn");
		try {
			JSONArray tArray=JsonUtil.formatRsToJsonArray(rs);
			jobj.put("tUserArray", tArray.toString());
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, rs);
			System.out.println("team Array "+jobj.toString());
		}
	}

}
